import { defineStore } from "pinia"
import {ref} from 'vue'
import request from '@/utils/request.js'

const registerService = (registerData) => {
    // 表单形式x-www.form-urlencoded，参数形式为请求体内键值对形式
    var uparams = new URLSearchParams();
    for (let dataKey in registerData) {
        uparams.append(dataKey, registerData[dataKey]);
    }
    return request.post('/user/register', uparams)
}


const  loginService = (loginData) => {

    var uparams = new URLSearchParams();
    for (let dataKey in loginData) {
        uparams.append(dataKey, loginData[dataKey]);
    }
    return request.post('/user/login', uparams)

}

export const getUserInfoService = () => {
    return request.get('/user/userInfo')
}

export const updateUserInfoService = (user) => {
    return request.put('/user/update',user)
}

export const updateAvatarService = (avatarUrl) => {
    // 第一个参数是URL，第二个参数是请求体（这里是空的），第三个参数是配置对象
    return request.patch('/user/updateAvatar', {},{params: {avatarUrl:  avatarUrl}})
}

export const useInfoStore = defineStore('userInfo',()=>{
    //1.定义用户信息
    const info = ref({})
    //2.定义修改用户信息的方法
    const setInfo = (newInfo)=>{
        info.value = newInfo
    }
    //3.定义清空用户信息的方法
    const removeInfo = ()=>{
        info.value={}
    }

    return{info,setInfo,removeInfo}
},{
    persist:true
})



// 导出默认
 export default {registerService,loginService}

